<?php

namespace App\Dao;

use App\Model\AnchorTask;
use App\Model\PayCall;
use App\Utils\FunUtil;
use Hyperf\Context\ApplicationContext;

class AnchorTaskDao
{
    public array $task = array(
        0 => array('complete' => 3600 * 3, 'money' => 10, 'title' => '在线时长', 'start' => 20, 'end' => 25),//20 25
        1 => array('complete' => 5, 'money' => 5, 'title' => '视频'),
        2 => array('complete' => 8, 'money' => 5, 'title' => '语音'),
    );

    private static $instance;

    public static function getInstance()
    {
        if (!self::$instance) {
            self::$instance = Applicationcontext::getcontainer()->get(self::class);
        }
        return self::$instance;
    }


    public function refresh2($uid, $type, $session_id = '')
    {
        if (intval(SettingDao::getInstance()->getOne('anchor_task_switch')) == 1) {
            $date_time = date('G') >= 6 ? date("Y-m-d") : date("Y-m-d", strtotime('-1 days'));
            // $one = $this->find('*', 'uid = ? and type = ? and status = ?', array($uid, $type, 1));
            $one = AnchorTask::query()->where('uid', $uid)->where('status', 1)->where('type', $type)->first();
            if (empty($one)) {
                // $model = $this->find('*', 'uid = ? and type = ? and date_time = ?', array($uid, $type, $date_time));
                $model = AnchorTask::query()->where('uid', $uid)->where('date_time', $date_time)->where('type', $type)->first();
                if (empty($model)) {
                    $insert = array(
                        'uid' => $uid,
                        'type' => $type,
                        'date_time' => date('G') >= 6 ? date("Y-m-d") : date("Y-m-d", strtotime('-1 days')),
                        'complete' => $this->task[$type]['complete'],
                        'money' => $this->task[$type]['money'],
                        'ctime' => time(),
                        'current' => 1,
                        'status' => 0,
                        'utime' => time(),
                        'end_date_time' => date('G') >= 6 ? date("Y-m-d") : date("Y-m-d", strtotime('-1 days'))
                    );
                    AnchorTask::query()->insertGetId($insert);
                } else {
                    $new['utime'] = time();
                    $date_time = strtotime($date_time);
                    if ($model['status'] == 0) {
                        $start = $date_time + 6 * 3600;
                        $end = $start + 24 * 3600;
                        if ($model['ctime'] >= $start) {
                            $start = $model['ctime'];
                        }
                        // $sql = "select count(DISTINCT user_id) as num from tb_pay_call where (status = 1 or status = 0) and end_time>0 and end_time-start_time >= 30 and  `type` =? and anchor_id = ? and ctime >= ? and ctime < ?";
                        // $pd = $this->createCommand($sql);
                        // $pd->execute(array($type, $uid, $start, $end));
                        // $num = $pd->fetch(PDO::FETCH_ASSOC);
                        $num = PayCall::query()->where('anchor_id', $uid)->where('type', $type)
                            ->where(function ($query) {
                                $query->where('status', 1)
                                    ->orWhere('status', 0);
                            })
                            ->where('end_time', '>', 0)
                            ->whereRaw('end_time - start_time >= 30')
                            ->whereBetween('ctime', [$start, $end])
                            ->distinct()
                            ->count('user_id');

                        $new['current'] = $num;
                        if ($new['current'] >= $this->task[$type]['complete']) {
                            // 没有实现不用调用了
                            // FunUtil::socket_client('anchor_task', $uid, $session_id, array('task_id' => $model['id'], 'task_type' => $type, 'money' => $model['money'], 'title' => $this->task[$type]['title']));
                            $new['status'] = 1;
                        }
                        AnchorTask::query()->where('id', $model['id'])->update($new);
                    }
                }
            }
        }
    }

    public function insertData($uid, $type)
    {

        $insert = array(
            'uid' => $uid,
            'type' => $type,
            'date_time' => date('G') >= 6 ? date("Y-m-d") : date("Y-m-d", strtotime('-1 days')),
            'complete' => $this->task[$type]['complete'],
            'money' => $this->task[$type]['money'],
            'ctime' => time(),
            'status' => 0,
            'current' => 0,
            'utime' => time(),
            'end_date_time' => date('G') >= 6 ? date("Y-m-d") : date("Y-m-d", strtotime('-1 days')),
        );
        return AnchorTask::query()->insert($insert);
    }


}